package com.vcom.deploy.cmd;

import org.noear.solon.core.message.Session;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class CmdThread extends Thread {

    private BufferedReader reader;
    private final Session session;
    private final InputStream inputStream;
    private final InputStream errorStream;

    public CmdThread(InputStream in, InputStream errorStream, Session session) {
        this.inputStream = in;
        this.errorStream = errorStream;
        this.session = session;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public InputStream getErrorStream() {
        return errorStream;
    }

    @Override
    public void run() {
        String line;
        try {
            this.reader = new BufferedReader(new InputStreamReader(getInputStream(), StandardCharsets.UTF_8));
            while ((line = reader.readLine()) != null) {
                session.send(line);
            }
            this.reader = new BufferedReader(new InputStreamReader(getErrorStream(), StandardCharsets.UTF_8));
            while ((line = reader.readLine()) != null) {
                session.send(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
